package middlewares

import (
	"gin_project/utils/jwt"
	"gin_project/utils/logs"
	"net/http"

	"github.com/gin-gonic/gin"
)

func JwtAuth(c *gin.Context) {
	resUrl := c.FullPath()
	logs.Debug(map[string]interface{}{"请求路径":resUrl},  "")    
	if resUrl == "/api/auth/login" || resUrl == "/api/auth/logout" {
		logs.Debug(map[string]interface{}{"请求路径":resUrl},  "login与logout不需要验证")	
		c.Next()
		return
	}

	tokenString := c.Request.Header.Get("token")
	if tokenString == "" {
		c.JSON(http.StatusOK, gin.H{
			"status":  401,
			"message": "请求未携带Token,请登录后尝试",
		})
		c.Abort()
		return
	}
	claim, err := jwt.ParseToken(tokenString)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"status":  401,
			"message": "Token验证未通过",
		})
		c.Abort()
		return
	}
	c.Set("claim", claim)
	c.Next()
}
